package com.yanqu.road.server.cmd.activity.tanghulu;

import com.yanqu.road.pb.activity.TangHuLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tanghulu.TangHuLuModule;
import com.yanqu.road.server.manger.activity.tanghulu.TangHuLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_TANGHULU_ACTIVITY_GAME_END, desc = "糖葫芦-主动结算")
public class TangHuLuGameEndCmd extends AbstractCommand {

    int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        TangHuLuProto.TangHuLuGameEndReqMsg reqMsg = TangHuLuProto.TangHuLuGameEndReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(TangHuLuMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!TangHuLuMgr.activityInTime()){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        if(type != 1 && type != 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        ret = player.getModule(TangHuLuModule.class).gameEnd(false, type);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            TangHuLuProto.TangHuLuGameEndRespMsg.Builder respMsg = TangHuLuProto.TangHuLuGameEndRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_TANGHULU_ACTIVITY_GAME_END, respMsg);
        }
    }
}
